<?php


namespace App\Services;


use Illuminate\Support\Facades\Cache;

class CodeService
{

    public function send($phone)
    {
        $code = $this->code($phone);
        if (app()->environment('testing')) return $code;
        app(TencentService::class)->sms(
            $phone,
            config('system.tencent_cloud.tencent_template_sign'),
            config('system.tencent_cloud.tencent_template_id'),
            [$code]
        );
    }

    protected function code($phone)
    {
        if (Cache::get($phone)) {
            abort(403, '请稍后再试');
        }
        $code = mt_rand(1000, 9999);
        Cache::put($phone, $code, config('system.code.time_out'));
        return $code;
    }
}
